﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Windows.Forms;
using System.Drawing;
using System.Net;

namespace Custom
{
    public class helper
    {
        private const string waitingId = "waitingmessage";

        public static string ApplicationPath()
        {
            return Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
        }

        public static string ApplicationCombinedPath(params string[] folders)
        {
            string tmpPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
            foreach (string folder in folders)
            {
                tmpPath = Path.Combine(tmpPath, folder);
            }

            return tmpPath;
        }

        public static XmlDocument XmlGetDocument(string documentPath)
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load(documentPath);
                return doc;
            }
            catch
            {
                return null;                
            }
        }

        public static XmlDocument XmlGetDocumentFromSource(string documentSource)
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.LoadXml(documentSource);
                return doc;
            }
            catch
            {
                return null;
            }
        }

        public static XmlNode XmlNewNodeFromSource(XmlDocument doc, string nodeSource)
        {
            XmlTextReader xmlReader = new XmlTextReader(new StringReader(nodeSource));
            return doc.ReadNode(xmlReader);
        }

        public static void FormDisable(Form sourceForm, string message)
        {
            sourceForm.Enabled = false;
            Label lblWait = new Label();
            lblWait.Text = message;
            lblWait.Name = waitingId;
            lblWait.ForeColor = Color.White;
            lblWait.BackColor = Color.Black;
            Control control = new Control();
            Graphics g = control.CreateGraphics();
            SizeF size = g.MeasureString(message, lblWait.Font);
            lblWait.TextAlign = ContentAlignment.TopCenter;
            lblWait.Size = new Size((int)size.Width + 15,(int)size.Height + 3);
            sourceForm.Controls.Add(lblWait);
            lblWait.Location = new Point((sourceForm.Size.Width / 2) - (lblWait.Size.Width / 2), (sourceForm.Size.Height / 2) - (lblWait.Size.Height / 2));
            lblWait.Visible = true;
            lblWait.BringToFront();
            sourceForm.Invalidate(lblWait.ClientRectangle);
            Application.DoEvents();
        }

        public static void FormEnable(Form sourceForm)
        {
            Label lblWait = null;
            foreach (Control control in sourceForm.Controls)
            {
                if (control.Name == waitingId)
                {
                    lblWait = (Label)control;
                    break;
                }
            }

            if (lblWait != null)
            {
                sourceForm.Controls.Remove(lblWait);
            }

            sourceForm.Enabled = true;
        }

        public static string getWebRequestResult(string URL)
        {
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
            myRequest.Method = "GET";
            WebResponse myResponse = myRequest.GetResponse();
            StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
            string result = sr.ReadToEnd();
            sr.Close();
            myResponse.Close();
            return result;
        }

        public static WebResponse getWebRequestResponse(string URL)
        {
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
            myRequest.Method = "GET";
            WebResponse myResponse = myRequest.GetResponse();
            return myResponse;
        }

        public static string getWebRequestResult(string URL, string user, string pass)
        {
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
            myRequest.Method = "GET";
            myRequest.Credentials = new NetworkCredential(user, pass);
            myRequest.PreAuthenticate = true;
            WebResponse myResponse = myRequest.GetResponse();
            StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
            string result = sr.ReadToEnd();
            sr.Close();
            myResponse.Close();
            //simyo post text j_username=morajuan&j_password=xxxxxxxx&x=85&y=9
            return result;
        }

        public static Image getWebRequestImage(string URL)
        {
            Bitmap b = null;

            WebResponse response = getWebRequestResponse(URL);
            b = new Bitmap(response.GetResponseStream());
            response.Close();

            return b;
        }

        public static System.Windows.Forms.ListView.ColumnHeaderCollection ColumnsAutoSize(System.Windows.Forms.ListView.ColumnHeaderCollection columnCollection)
        {
            foreach (ColumnHeader column in columnCollection)
            {
                column.Width = -1;
            }
            return columnCollection;
        }
    }
}
